{extend name="public/baseLayuimini"}
{block name="title"}{/block}
{block name="css"}
    <style>
        .layui-btn:not(.layui-btn-lg ):not(.layui-btn-sm):not(.layui-btn-xs) {
            height: 34px;
            line-height: 34px;
            padding: 0 8px;
        }
    </style>
{/block}
{block name="main"}
<div class="layuimini-container">
    <div class="layuimini-main">
        <blockquote class="layui-elem-quote">
            栏目管理只针对开发者,管理员请勿修改,如果修改,可能将来会有未知的风险,请悉知。<br>
        </blockquote>
        <div>
            <div class="layui-btn-group">
                <button class="layui-btn" id="btn-expand">全部展开</button>
                <button class="layui-btn layui-btn-normal" id="btn-fold">全部折叠</button>
                <button class="layui-btn layui-btn-normal" id="btn-add">新增顶级</button>
            </div>
            <table id="munu-table" class="layui-table" lay-filter="munu-table"></table>
        </div>
    </div>
</div>
{/block}
{block name="js"}
<!-- 操作列 -->
<script type="text/html" id="auth-state">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="addChild">子菜单</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
<script src="__LAYUIMINI__/js/lay-config.js?v=1.0.4" charset="utf-8"></script>
<script>
    layui.use(['table', 'treetable','layer'], function () {
        var $ = layui.jquery;
        var table = layui.table;
        var treetable = layui.treetable;
        var layer = layui.layer;

        // 渲染表格
        var insTb = treetable.render({
            treeColIndex: 1,
            treeSpid: 1,
            treeIdName:'id',
            treePidName:'pid',
            treeDefaultClose:false,
            treeLinkage:true,
            elem: '#munu-table',
            url: '/system/systemMenu',
            page: false,
            cols: [[
                {type: 'numbers'},
                {field: 'title', minWidth: 200, title: '名称'},
                {field: 'href', title: 'url地址(理论上说如果有子菜单，则父级不存在url地址，因为不会生效)',},
                {
                    field: 'icon', width: 80, align: 'center', templet: function (d) {
                        return `<span class="${d.icon}"></span>`;
                    }, title: '图标'
                },
                {field: 'status', width: 80, align: 'center', title: '状态', templet:function(d){
                        return `<input type="checkbox" name="status" value="${d.status}" lay-skin="switch" lay-text="开|关" lay-filter="status" ${d.status == 1 ? 'checked' : '' }>`
                    }},
                {
                    field: 'target', width: 80, align: 'center', templet: function (d) {
                        if (d.target == '_self') {
                            return '<span class="layui-badge layui-bg-gray">iframe</span>';
                        }
                        if (d.target == '_target') {
                            return '<span class="layui-badge layui-bg-blue">新页面</span>';
                        }
                    }, title: '类型'
                },
                {templet: '#auth-state', width: 180, align: 'center', title: '操作'}
            ]],
        });
        $('#btn-expand').click(function () {
            treetable.expandAll('#munu-table');
        });

        $('#btn-fold').click(function () {
            treetable.foldAll('#munu-table');
        });
        //添加顶级菜单
        $('#btn-add').click(function () {
            layer.open({
                type: 2,
                content: '/system/editSystemMenu',
                title:"编辑按钮",
                area:['700px','450px']
            });
        });
        //监听工具条
        table.on('tool(munu-table)', function (obj) {
            var data = obj.data;
            var layEvent = obj.event;
            if (layEvent === 'del') {
                var index = layer.load()
                $.get('/system/delMenu',{id:data.id},res=>{
                    layer.close(index)
                    if(res.code == 200){
                        layer.msg(res.msg)
                        location.reload(true);
                    }else{
                        layer.msg(res.msg)
                    }

                })
            } else if (layEvent === 'edit') {
                layer.open({
                    type: 2,
                    content: '/system/editSystemMenu?id='+data.id,
                    title:"编辑按钮",
                    area:['700px','450px']
                });
            }else if(layEvent === 'addChild'){
                layer.open({
                    type: 2,
                    content: '/system/editSystemMenu?pid='+data.id,
                    title:"编辑按钮",
                    area:['700px','450px']
                });
            }
        });
    });
</script>
{/block}